package com.server;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.Helper.CustomerHelper;
import com.Helper.FlightHelper;
import com.Helper.FlightInfo;
import com.Helper.ReservationHelper;
import com.Helper.ReservationInfo;
import com.Helper.SeatHelper;
import com.Helper.SeatInfo;


public class ViewReservationServlet extends CustomerServlet
{
	private static final long serialVersionUID = 1L;
	private final String className = "Customer Login Servlet: ";
	String msg = null;

	@Override
	//@requires HttpServletRequest !=NULL,  HttpServletResponse !=NULL ;
	//@signals_only RuntimeException, IOException,ServletException;
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException
	{
		ConnectDB();
		
		if (req.getParameter("view") != null)
		{
			View(req, resp);
		}
		else if (req.getParameter("remove") != null)
		{
			Remove(req, resp);
		}
		else
		{
			System.out.println("ViewReservationServlet: Did you add extra buttons?");
		}
		
	}
	//@requires HttpServletRequest !=NULL,  HttpServletResponse !=NULL ;
	//@assignable username,resID,msg;
	//@signals_only RuntimeException, IOException,ServletException;
	private void Remove(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
	{
		String username = req.getParameter("username");
		String resID = req.getParameter("r1");
		
		req.setAttribute("username", username);	
		req.setAttribute("resID", resID);	
		
		try 
		{
			if (NaN(resID))//check the reservation ID
			{
				msg = "Invalid Reservation ID";
				req.setAttribute("msg", msg);
				req.getRequestDispatcher("reservation/CancelResResult.jsp").forward(req, resp);
			}
			else
			{
			
				ReservationHelper.Setup(connection, username);
				CustomerHelper.Setup(connection);
				SeatHelper.Setup(connection);
				FlightHelper.Setup(connection);
				
				ReservationInfo resInfo = ReservationHelper.GetSelectedReservation(Integer.parseInt(resID));
								
				msg = "Reservation ID " + resInfo.num + " cancelled. $" + resInfo.paidPrice + " and " 
						+ resInfo.paidPoint + " points have been refunded"; 
				
				CustomerHelper.AddPoint(username, resInfo.paidPoint);
				SeatHelper.MarkEmpty(resInfo.seat, resInfo.flightID);
				ReservationHelper.CancelReservation(resID);
				req.setAttribute("msg", msg);
				req.getRequestDispatcher("reservation/CancelResResult.jsp").forward(req, resp);
			}	
		} 
		catch (SQLException e) 
		{

			e.printStackTrace();
		}
	}
	//@requires HttpServletRequest !=NULL,  HttpServletResponse !=NULL ;
    //@assignable resID,flightInfo,resInfo,seatInfo,username;
	//@signals_only RuntimeException, IOException,ServletException;
	private void View(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
	{
		FlightInfo flightInfo = null;
		ReservationInfo resInfo = null;
		SeatInfo seatInfo = null;
		String username = req.getParameter("username");
		String sResID = req.getParameter("r1");
		
		if (sResID == null)
		{
			msg = "You did not select a reservation to view.";
			req.setAttribute("msg", msg);
			req.getRequestDispatcher("reservation/error.jsp").forward(req, resp);
		}
		else
		{
		
			int resID = Integer.parseInt(req.getParameter("r1"));
			
			try //get resInfo,flightInfo and seatInfo then display them
			{
				ReservationHelper.Setup(connection, username);
				CustomerHelper.Setup(connection);
				SeatHelper.Setup(connection);
				
				resInfo = ReservationHelper.GetSelectedReservation(resID);
				flightInfo = ReservationHelper.GetSelectedFlight(resInfo.flightID);
				seatInfo = SeatHelper.GetSelectedSeat(resInfo.seat, flightInfo.flightID);
				
				int point = CustomerHelper.GetSelectedCustomer(username).point;
				
				req.setAttribute("seatInfo", seatInfo);
				req.setAttribute("resInfo", resInfo);
				req.setAttribute("flightInfo", flightInfo);
				req.setAttribute("point", point);
				req.setAttribute("username", username);
				req.getRequestDispatcher("reservation/viewReservation.jsp").forward(req, resp);
			} 
			catch (SQLException e) 
			{
	
				e.printStackTrace();
			}
		}
	}

}

